package tp.pr3.instructions;


import java.util.StringTokenizer;

import tp.pr3.NavigationModule;
import tp.pr3.RobotEngine;
import tp.pr3.instructions.exceptions.*;
import tp.pr3.items.ItemContainer;

public class MoveInstruction
extends java.lang.Object
implements Instruction{
	
	private RobotEngine engine;
    private NavigationModule navigation;

	
	public Instruction parse(java.lang.String cad) throws WrongInstructionFormatException
	{
		StringTokenizer instructionToken = new StringTokenizer(cad);

		if (instructionToken.countTokens() != 1)
			throw new WrongInstructionFormatException();
		
		String actionString = instructionToken.nextToken();
				
		if(!(actionString.equalsIgnoreCase("MOVE") || actionString.equalsIgnoreCase("MOVER")))
			throw new WrongInstructionFormatException();		
				
		return this;		
 	}
	
	public void execute()throws InstructionExecutionException
	{
		
	try
		{
		navigation.move();
		engine.addFuel(-5);
		navigation.scanCurrentPlace();
		System.out.println("");
		engine.printRobotState();
		
		}catch (Exception e)
		{
			throw new InstructionExecutionException(e.getMessage());
		}
	
	  if(navigation.atSpaceship())
	  {		  
		  gameOverMessage();
		  engine.requestQuit();		  
	  }
	}
	
	
	public java.lang.String getHelp()
	{
		return ("MOVE | MOVER");
	}
	
	public void configureContext(RobotEngine engine,
            NavigationModule navigation,
            ItemContainer robotContainer)
	{
		this.engine=engine;
		this.navigation=navigation;
	}
	
	private void gameOverMessage()
	{		
	System.out.println("WALL·E says: I am at my spaceship. Bye bye");
	}
}
